package com.socodethis.spaceex;

import java.util.ArrayList;
import java.util.Collections;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

public class texturePackerLoader {
	TextureAtlas atlas;
	public ArrayList <spriteSheet> sprites;
	private int i;
	Texture spriteMap;
	ArrayList <Sprite> holder;
	public texturePackerLoader(String textFile, String imageFile, float rotation){
		atlas = new TextureAtlas(textFile);
		sprites = new ArrayList <spriteSheet>();
		spriteMap = new Texture(Gdx.files.internal(imageFile));
		seperateSprites(rotation);
	}
	private void seperateSprites(float rotation){
		int count = 0;
		Sprite holderSprite;
		//Iterates through all of the regions in the atlas and cuts them out of the spriteMap
		//Everything is stored in the sprites arrayList
		for(AtlasRegion region: atlas.getRegions()){
			holderSprite = new Sprite(spriteMap,region.getRegionX(),region.getRegionY(),
					region.getRegionWidth(),region.getRegionHeight());
			holderSprite.rotate(rotation);
			sprites.add(new spriteSheet(holderSprite,region.name));
			//System.out.println(region.getRegionX() +":"+region.getRegionY()+":"
			//		+(region.getRegionX()+region.getRegionWidth())+":"+(region.getRegionY()+region.getRegionHeight()));
			if(region.name.indexOf("wall")!=-1){
				sprites.get(count).setPassable(false);
			}
			count++;
		}
		//System.out.println("Size: "+ sprites.size());
	}
	//This is a seperate class so that properties can be set for the individual tiles
	public class spriteSheet{
		Sprite sprite;
		int danger, difficulty;
		boolean passable;
		String name;
		spriteSheet(Sprite sprite, String name){
			this.sprite = new Sprite(sprite);
			this.name = name;
			passable = true;
			danger = 0;
			difficulty = 0;
		}
		public Sprite getSprite(){
			return this.sprite;
		}
		public void setPassable(boolean passable){
			this.passable = passable;
		}
		public boolean getPassable(){
			return passable;
		}
		public String getName(){
			return name;
		}
	}
	//Returns a sprite with the specified name
	public Sprite getName(String name){
		holder = new ArrayList <Sprite>();
		for(i = 0; i<sprites.size();i++){
			//System.out.println("Name: " + name + " Looking: " + sprite.getName());
			if(sprites.get(i).getName().indexOf(name)!=-1){
				holder.add(sprites.get(i).getSprite());
			}
		}
		if(holder.size() > 0){
			Collections.shuffle(holder);
			return holder.get(0);
		}
		else{
			return sprites.get(1).getSprite();
		}
	}
}
